<?php 
class MyshopAction extends Action{
	
    public function _initialize() {
	}
	
	public function index() {
		
		$goods = M ( 'goods' );
		$count = $goods->where( 'uid='.$this->mid )->count();    //计算总数
		$p = new Page ( $count, 15 );
		$goodslist=$goods->where ( 'uid='.$this->mid )->limit($p->firstRow.','.$p->listRows)->order('utime DESC')->findAll ();
	    $page = $p->show ();
		$this->assign ( "page", $page );
	    $this->assign('goods',$goodslist);
	    $this->setTitle('我是卖家');
		$this->display();
	}
	
	public function edit(){
		$gid  = empty($_GET['id'])? 0 : intval($_GET['id']);
		if($gid == 0){
	    	$this->error("错误的信息地址.请检查后再访问");
            exit;
	    }
		$sql = array();
		$sql['id']=$gid;
		$sql['uid']=$this->mid;
		
		$goods = M ( 'goods' )->where ($sql)->find();
		//print_r($goods);
		if(!$goods){
		   $this->error('抱歉，你所查看的内容不存在，或者已经删除！');	
		   exit;
		}
			
		$this->assign('goods',$goods);
		$type = M ( 'goods_type' );
		$goodstype=$type->order('display_id')->findAll();
		//print_r($goodstype);
		$this->assign('goodstype',$goodstype);
		$this->setTitle($goods['title'].' - 编辑商品');
		$this->display('editgoods');	
		
		
	}
	public function doDel(){
		$id  =  empty($_POST['id'])?  0 : intval($_POST['id']);
		if(!$id){
		 echo 0;
		 return;
		}   $map = $map2 =array();
			$map['id']  = $id;
			$map['uid'] = $this->mid;
			$goods =M('goods');
			$res =$goods->field('gimage' )->where($map)->find();
			if($res['gimage']){
			   @unlink(SITE_PATH.'/data/uploads/goods/'.$res['gimage']);
			   @unlink(SITE_PATH.'/data/uploads/goods/m_'.$res['gimage']);
			   @unlink(SITE_PATH.'/data/uploads/goods/s_'.$res['gimage']);
			}
			$rel = $goods->where($map)->delete();
			$map2['shop_id']  = $this->mid;
			$map2['goods_id'] = $id;
			$map2['type']     = 'goods';
			M('shop_leaveword')->where($map2)->delete();	
			echo ($res = $rel)? 1:0;
		
	}
	public function add(){
		
		$type = M('goods_type');
		$goodstype=$type->order('display_id')->findAll();
		$userinfo = M('user_basicfile')->field('province, city, school')->where('uid='.$this->mid)->find();
		$this->assign('userinfo',$userinfo);
		$this->assign('goodstype',$goodstype);
		$this->setTitle('发布新商品');
		$this->display('editgoods');
		
	}
    public function dochekall(){
    	if($_POST['title']==''){
    		
    	   $this->error('标题不能为空');
    	}elseif($_POST['name']==''){
    		$this->error('商品名称不能为空');
    	}elseif($_POST['g_details']==''){
    	   $this->error('详细内容不能为空');		
    	}elseif($_POST['area_province']==0 || $_POST['area_city']==0){
		   $this->error('请选择城市');
		}else{
    	$this->saveform();	
    		
    	}
    	
    }
	protected function saveform(){
		if($_POST['id']){
		   $goods = D ('goods');
		  $data=array();
		  $data['id']          =  intval($_POST['id']);
		   if($_FILES['image']['name']) {
			//如果有文件上传 上传附件
			$res = $goods->field('gimage')->where('id='.$data['id'])->find();
			@unlink(SITE_PATH.'/data/uploads/goods/'.$res['gimage']);
			@unlink(SITE_PATH.'/data/uploads/goods/m_'.$res['gimage']);
			@unlink(SITE_PATH.'/data/uploads/goods/s_'.$res['gimage']);
			$this->_upload();
			  if($_POST['image']){
				$data['gimage']=$_POST['image'];
			    }
		    }	
		  
		  $data['title']       =  t($_POST['title']);
		  $data['gname']       =  t($_POST['name']);
		  $data['gprice']      =  t($_POST['price']);
		  $data['gnumber']     =  t($_POST['number']);
		  $data['gtype']       =  intval($_POST['type']);
		  $data['province']    =  intval($_POST['area_province']);
		  $data['city']        =  intval($_POST['area_city']);
		  $data['location']    =   getLocation($data['province'],$data['city']);
		  $data['school']      =  $_POST['school'];
		  $data['gdetails']    =  h(keyWordFilter($_POST['g_details']));
		  $data['utime']       =  time();
		
		 if($goods->save($data)) {
    			$this->success('数据更新成功！');
          }else{
            	//echo $goods->getLastSql(); 
                $this->error('数据更新失败');
          }
		
		}else{
			
		   $goods = D ('goods');	
		   $data=array();
		    if(!empty($_FILES['image']['name'])) {
			//如果有文件上传 上传附件
			$this->_upload();
		    if($_POST['image']){
				$data['gimage']=$_POST['image'];
			    }
		    }
          
		    $data['uid']         =   $this->mid;
			$data['uname']       =   $this->user['uname'];
		    $data['title']       =   t($_POST['title']);
		    $data['gname']       =   t($_POST['name']);
		    $data['gprice']      =   t($_POST['price']);
		    $data['gnumber']     =   t($_POST['number']);
		    $data['gtype']       =   intval($_POST['type']);
		    $data['province']    =   intval($_POST['area_province']);
		    $data['city']        =   intval($_POST['area_city']);
		    $data['location']    =   getLocation($data['province'],$data['city']);
		    $data['school']      =   $_POST['school'];
		    $data['gdetails']    =   h(keyWordFilter($_POST['g_details']));
		    $data['utime']       =  $data['ctime']   =   time();
			
		    if(false !==$goods->add($data)) {
    	     $this->success('数据添加成功！');
              }else{
             	//echo $goods->getLastSql(); 
                $this->error('数据写入错误');
                //Exit;
               
              }
		   
		}
		
	}
	
// 文件上传
    protected function _upload()
    {
        //import("@.ORG.UploadFile");
        include( SITE_PATH.'/addons/libs/UploadFile.php' );
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize  = 3292200 ;
        //设置上传文件类型
        $upload->allowExts  = explode(',','jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath =  UPLOAD_PATH.'/goods/';
	    //设置需要生成缩略图，仅对图像文件有效
       $upload->thumb =  true;
       //设置需要生成缩略图的文件后缀
	    $upload->thumbPrefix   =  'm_,s_';  //生产2张缩略图
       //设置缩略图最大宽度
		$upload->thumbMaxWidth =  '200,100';
       //设置缩略图最大高度
		$upload->thumbMaxHeight = '200,100';
	   //设置上传文件规则
	   $upload->saveRule = uniqid;
	   //删除原图
	   $upload->thumbRemoveOrigin = false;
        if(!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        }else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $_POST['image']  = $uploadList[0]['savename'];
           
        }
        
	}
	
}



?>